{"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\nexports.useUploadCreator = void 0;\nconst useUploadCreator = _ref => {\n  let {\n    beforeRequest,\n    setState,\n    updateState\n  } = _ref;\n  const upload = async _ref2 => {\n    let {\n      files\n    } = _ref2;\n    setState({\n      loading: true,\n      done: false\n    });\n    const xhr = new XMLHttpRequest();\n    let options = await beforeRequest({\n      xhr,\n      files\n    }); // bail out if you return undefined from options\n\n    if (!options) return setState({\n      loading: false,\n      done: false\n    });\n    xhr.open(options.method, options.url);\n    /*\n      Helper method for setting headers on an xhr request, one of the only\n      extra features of this hook\n    */\n\n    if (options.headers) {\n      let headers = options.headers;\n      Object.keys(options.headers).forEach(header => xhr.setRequestHeader(header, headers[header]));\n    }\n    /*\n      XHR Listeners\n    */\n\n    xhr.upload.addEventListener(\"progress\", event => {\n      updateState(state => ({\n        ...state,\n        progress: Math.round(event.loaded / event.total * 100)\n      }));\n    });\n    xhr.addEventListener(\"load\", () => {\n      let data;\n      try {\n        data = JSON.parse(xhr.response);\n      } catch (e) {\n        data = xhr.response;\n      }\n      let responseHeaders = xhr.getAllResponseHeaders().trim().split(/[\\r\\n]+/).map(line => line.split(\": \")).reduce((acc, _ref3) => {\n        let [header, value] = _ref3;\n        acc[header] = value;\n        return acc;\n      }, {});\n      setState({\n        data,\n        loading: false,\n        xhr,\n        responseHeaders,\n        done: true\n      });\n    });\n    xhr.addEventListener(\"error\", error => {\n      setState({\n        error,\n        xhr,\n        loading: false,\n        done: true\n      });\n    });\n    xhr.addEventListener(\"abort\", error => {\n      setState({\n        error,\n        xhr,\n        loading: false,\n        done: true\n      });\n    });\n    /*\n      send the request!\n    */\n\n    xhr.send(options.body);\n  };\n  return upload;\n};\nexports.useUploadCreator = useUploadCreator;","map":{"version":3,"names":["Object","defineProperty","exports","value","useUploadCreator","beforeRequest","setState","updateState","upload","files","loading","done","xhr","XMLHttpRequest","options","open","method","url","headers","keys","forEach","header","setRequestHeader","addEventListener","event","state","progress","Math","round","loaded","total","data","JSON","parse","response","e","responseHeaders","getAllResponseHeaders","trim","split","map","line","reduce","acc","error","send","body"],"sources":["/data/messenger.client/node_modules/@zach.codes/use-upload/lib/library/use-upload.js"],"sourcesContent":["\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\nexports.useUploadCreator = void 0;\n\nconst useUploadCreator = ({\n  beforeRequest,\n  setState,\n  updateState\n}) => {\n  const upload = async ({\n    files\n  }) => {\n    setState({\n      loading: true,\n      done: false\n    });\n    const xhr = new XMLHttpRequest();\n    let options = await beforeRequest({\n      xhr,\n      files\n    }); // bail out if you return undefined from options\n\n    if (!options) return setState({\n      loading: false,\n      done: false\n    });\n    xhr.open(options.method, options.url);\n    /*\n      Helper method for setting headers on an xhr request, one of the only\n      extra features of this hook\n    */\n\n    if (options.headers) {\n      let headers = options.headers;\n      Object.keys(options.headers).forEach(header => xhr.setRequestHeader(header, headers[header]));\n    }\n    /*\n      XHR Listeners\n    */\n\n\n    xhr.upload.addEventListener(\"progress\", event => {\n      updateState(state => ({ ...state,\n        progress: Math.round(event.loaded / event.total * 100)\n      }));\n    });\n    xhr.addEventListener(\"load\", () => {\n      let data;\n\n      try {\n        data = JSON.parse(xhr.response);\n      } catch (e) {\n        data = xhr.response;\n      }\n\n      let responseHeaders = xhr.getAllResponseHeaders().trim().split(/[\\r\\n]+/).map(line => line.split(\": \")).reduce((acc, [header, value]) => {\n        acc[header] = value;\n        return acc;\n      }, {});\n      setState({\n        data,\n        loading: false,\n        xhr,\n        responseHeaders,\n        done: true\n      });\n    });\n    xhr.addEventListener(\"error\", error => {\n      setState({\n        error,\n        xhr,\n        loading: false,\n        done: true\n      });\n    });\n    xhr.addEventListener(\"abort\", error => {\n      setState({\n        error,\n        xhr,\n        loading: false,\n        done: true\n      });\n    });\n    /*\n      send the request!\n    */\n\n    xhr.send(options.body);\n  };\n\n  return upload;\n};\n\nexports.useUploadCreator = useUploadCreator;"],"mappings":"AAA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